//
//  ZYCModifyPasswordViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/20.
//

#import "ZYCModifyPasswordViewController.h"
#import "ZYCModifyPassView.h"
#import "BTEnterErrorDispose.h"
#import "NSString+File.h"
#import "ZYCNavigationController.h"
#import "ZYCLoginViewController.h"
@interface ZYCModifyPasswordViewController ()

@property (nonatomic, strong) UIButton *saveButton;
@property (nonatomic, strong) ZYCModifyPassView *modifyPassView;
@property (nonatomic, copy) NSString *roleType;
@end

@implementation ZYCModifyPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"修改密码";
    
    NSInteger roleType =  [[NSUserDefaults standardUserDefaults] integerForKey:@"kUserRoleType"];
    if (roleType == 0) {
        self.roleType = @"1";
    }else{
        self.roleType = @"2";
    }
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.saveButton];
    [self.view addSubview:self.modifyPassView];
    [self.modifyPassView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(kNavBarAndStatusBarHeight);
    }];
}
- (UIButton *)saveButton {
    if (!_saveButton) {
        _saveButton = [[UIButton alloc] init];
        [_saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [_saveButton setTitleColor:HEX_RGB(kBgWhiteColor) forState:UIControlStateNormal];
        _saveButton.titleLabel.font = kCustomFont(@"PingFangSC-Regular", 16.0f);
        [_saveButton addTarget:self action:@selector(clickSaveAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveButton;
}

- (void)clickSaveAction:(UIButton *)sender{
    [self modifyPaaaword];
}

- (void)modifyPaaaword{
    NSString *oldPassword =self.modifyPassView.oldPassword;
    NSString *newPassword =self.modifyPassView.newPassword;
    NSString *surePassword =self.modifyPassView.surePassword;
    if (oldPassword.length == 0) {
        [MrjToast makeToast:@"请输入旧密码" makeView:self.view];
        return;
    }
    if (newPassword.length == 0) {
        [MrjToast makeToast:@"请输入新密码" makeView:self.view];
        return;
    }
    if (surePassword.length == 0) {
        [MrjToast makeToast:@"请再次输入新密码" makeView:self.view];
        return;
    }

    if (![newPassword isEqualToString:surePassword]) {
        [MrjToast makeToast:@"两次密码密码不一致" makeView:self.view];
        return;
    }
    
    if ([NSString stringContainsEmoji:oldPassword]||[NSString stringContainsEmoji:newPassword]||[NSString stringContainsEmoji:surePassword]) {
        [MrjToast makeToast:@"不能输入表情" makeView:self.view];
        return;
    }
    
    if (newPassword.length < 6|| surePassword.length < 6){
        [MrjToast makeToast:@"密码应为6~20位数字、字母和特殊符号组成" makeView:self.view];
        return;
    }
    
    if (newPassword.length  > 20|| surePassword.length > 20){
        [MrjToast makeToast:@"密码应为6~20位数字、字母和特殊符号组成" makeView:self.view];
        return;
    }

    NSDictionary *params = @{
        @"oldPassword":self.modifyPassView.oldPassword,
        @"newPassword":self.modifyPassView.newPassword,
        @"roleType":self.roleType
    };
    //
    [MrjSVProgressHUD showWithStatus:@"修改中..."];
    [[URLManager sharedURLManager] postUrlString:Profile_Post_ModifyUserInfo requestService:RequestServiceSystem parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                [MrjSVProgressHUD showSuccessWithStatus:@"修改成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
                    ZYCLoginViewController *vc = [[ZYCLoginViewController alloc]init];
                    ZYCNavigationController *nav = [[ZYCNavigationController alloc]initWithRootViewController:vc];
                    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
                    
                });
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            [MrjToast makeToast:@"网络异常" makeView:self.view];
        }];
    
}

- (ZYCModifyPassView *)modifyPassView {
    if (!_modifyPassView) {
        _modifyPassView = [[ZYCModifyPassView alloc] init];

    }
    return _modifyPassView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
